package cn.pconline.autoclub.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.pconline.autoclub.entity.Activity;
import cn.pconline.autoclub.entity.ActivityPlace;

/**
 * 
 * @author zhoutianhuai
 *
 */
public class ActivityPlaceService {
	 @Autowired
	 private GeliDao geliDao;
	 @Autowired
	 private GeliOrm geliOrm;
	 private static final Logger log = LoggerFactory.getLogger(ActivityPlaceService.class);
	 @Autowired
	 private JdbcTemplate jdbcTemplate;
	 
	 /**
	  * 创建活动地点
	  * @param activityPlace
	  * @return
	  */
	 public long createActivityPlace(ActivityPlace activityPlace){
		  return geliDao.create(activityPlace);
	 }
	 
	 /**
	  * 批量创建活动地点
	  * @param activityPlaces
	  */
	 public int batchCreateActivityPlace(long activityId, List<ActivityPlace> activityPlaces){ 	 
	 	 if( activityPlaces == null || activityPlaces.isEmpty()){
	         return 0;
	      }
	 	  SqlBuilder sqlBuilder = new SqlBuilder();
	 	  sqlBuilder.appendSql(" insert into ").appendSql(geliOrm.getTableName(ActivityPlace.class));
	 	  sqlBuilder.appendSql(" (place_id,activity_id,type,name,longitude,latitude,seq,mark_name) value(?,?,?,?,?,?,?,?)");
	 	  List<Object[]> batch = new ArrayList<Object[]>();
	 	 int i = 1;
	 	  for(ActivityPlace place : activityPlaces){
	 		 Object[] values = new Object[]{geliDao.generateId(ActivityPlace.class),activityId,place.getType(),place.getName(),place.getLongitude(),place.getLatitude(),i++,place.getMarkName()};
	 		 batch.add(values);
	 	  }
	 	  return jdbcTemplate.batchUpdate(sqlBuilder.getSql(),batch).length;

	 }
	 
	 /**
	  * 更新活动地点
	  * @param activityPlace
	  * @return
	  */
	 public boolean updateActivityPlace(ActivityPlace activityPlace){
		  return geliDao.update(activityPlace) >0;
	 }
	 
	 /**
	  * 更新活动地点
	  * @param activityPlaces
	  * @return
	  */
	 public boolean updateActivityPlace(long activityId, List<ActivityPlace> activityPlaces){
		 boolean flag = false;
		 try {
			for (ActivityPlace place : activityPlaces) {
				  ActivityPlace place2 = geliDao.find(ActivityPlace.class,
						activityId);
				if (place2 == null) {
					 createActivityPlace(place);
				} else {
					 updateActivityPlace(place);
				}
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
		}
		  
		return flag;
	 }
	 
	/**
	 * 先删除地点再创建
	 * 
	 * @param activityId
	 * @param pics
	 * @return
	 */
	public int deleteAndAddActivityPlace(long activityId,
			List<ActivityPlace> pics) {
		deleteActivityPlacesByActivityId(activityId);
		return batchCreateActivityPlace(activityId, pics);
	}

	/**
	 * 根据activityId删除所属的活动地址
	 * 
	 * @param activityId
	 */
	public int deleteActivityPlacesByActivityId(long activityId) {
		SqlBuilder sqlBuilder = new SqlBuilder();
		sqlBuilder.appendSql(" delete from ").appendSql(
				geliOrm.getTableName(ActivityPlace.class));
		sqlBuilder.appendSql(" where activity_id = ").appendValue(activityId);
		return geliDao.getJdbcTemplate().update(sqlBuilder.getSql(),
				sqlBuilder.getValues());
	}

	/**
	 * 根据活动Id获取活动地点列表
	 * 
	 * @param activityId
	 * @return
	 */
	public List<ActivityPlace> getActivityPlaceByActivityId(long activityId) {
		SqlBuilder sqlBuilder = new SqlBuilder();
		sqlBuilder.appendSql("select place_id from ").appendSql(
				geliOrm.getTableName(ActivityPlace.class));
		sqlBuilder.appendSql(" where activity_id = ").appendValue(activityId);
		sqlBuilder.appendSql(" order by seq");
		try {
			return geliDao.list(ActivityPlace.class, sqlBuilder.getSql(),
					sqlBuilder.getValues());
		} catch (EmptyResultDataAccessException e) {
			log.info("[activityId]({})对应的活动地点不存在", activityId);
			return null;
		}
	}

	/**
	 * 根据活动Id获取活动地点
	 * 
	 * @param activityId
	 * @return
	 */
	public ActivityPlace getActivityPlace(long activityId) {
		try {
			return geliDao.find(ActivityPlace.class, activityId);
		} catch (EmptyResultDataAccessException e) {
			log.info("[activityId]({})对应的活动地点不存在", activityId);
			return null;
		}

	}

	/**
	 * 根据活动获取集合地点或目的地点
	 * 
	 * @param activity
	 * @param type
	 *            地点类型
	 * @return
	 */
	public List<ActivityPlace> listPlaceByActivityAndType(Activity activity,
			int type) {
		if (activity == null)
			return Collections.emptyList();
		SqlBuilder sqlBuilder = new SqlBuilder();
		sqlBuilder.appendSql(" select place_id from ").appendSql(
				geliOrm.getTableName(ActivityPlace.class));
		sqlBuilder.appendSql(" where activity_id = ").appendValue(
				activity.getActivityId());
		sqlBuilder.appendSql(" and type = ").appendValue(type);
		sqlBuilder.appendSql(" order by seq");
		try {
			return geliDao.list(ActivityPlace.class, sqlBuilder.getSql(),
					sqlBuilder.getValues());
		} catch (EmptyResultDataAccessException e) {
			log.info("[activityId]({})对应的活动地点不存在", activity.getActivityId());
			return Collections.emptyList();
		}

	}
}
